package 力扣.二分;

import java.util.Arrays;

public class 查找多个重复元素34 {
    public static void main(String[] args) {
        int []nums={5,7,7,8,8,10};
        int []nums2={};
        int[] ints = searchRange(nums, 8);
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);
        }
    }
    public static int[] searchRange(int[] nums, int target) {
        if (nums == null || nums.length == 0){
              return new int[]{-1,-1};
        }
        int n = nums.length;
        int l = lowerBound(nums, n, target);
        int r = upperBound(nums, n, target);
        r--;
        return l < n && r < n && nums[l] == target && nums[r] == target
                ? new int[]{l,r} : new int[]{-1,-1};
    }
    private static int lowerBound(int[] A,int n ,int target){
        int l = 0,r = n;
        while (l < r){
            int mid = l + ((r - l) >> 1);
            if (A[mid] < target){
                l = mid + 1;
            }else {
                r = mid;
            }
        }
        return l;
    }
    private static int upperBound(int[] A,int n ,int target){
        int l = 0,r = n;
        while (l < r){
            int mid = l + ((r - l) >> 1);
            if (A[mid] <= target){
                l = mid + 1;
            }else {
                r = mid;
            }
        }
        return l;
    }


}
